package com.acane.instagram.feature.me

import android.util.Log
import androidx.compose.foundation.Image
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.heightIn
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.Divider
import androidx.compose.material3.Button
import androidx.compose.material3.ButtonDefaults
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.SnackbarHost
import androidx.compose.material3.SnackbarHostState
import androidx.compose.material3.Text
import androidx.compose.material3.TextField
import androidx.compose.material3.TextFieldDefaults
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.input.PasswordVisualTransformation
import androidx.compose.ui.text.input.VisualTransformation
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.acane.instagram.R
import com.acane.instagram.core.design.theme.InstagramBlue
import com.acane.instagram.core.design.theme.InstagramDarkGray
import com.acane.instagram.core.design.theme.LightGrayBackground
import com.acane.instagram.core.model.UserInfo


@Composable
fun Login1(loggedAccountList: MutableList<UserInfo>) {
    var inputUserName by remember { mutableStateOf("") }
    var inputPassword by remember { mutableStateOf("") }
    val snackbarHostState = remember { SnackbarHostState() }
    var showSnackbar by remember { mutableStateOf(false) }

    Column(
        horizontalAlignment = Alignment.CenterHorizontally,
        verticalArrangement = Arrangement.Center,
        modifier = Modifier.padding(16.dp)
    ) {
        // LOGO
        LOGO1()
        // 账号密码输入区域
        LoginInput1(
            inputUserName = inputUserName,
            onUserNameChange = { inputUserName = it },
            inputPassword = inputPassword,
            onPasswordChange = { inputPassword = it }
        )
        // 忘记密码文本
        Row(
            modifier = Modifier
                .fillMaxWidth()
                .padding(top = 10.dp),
            horizontalArrangement = Arrangement.End
        ) {
            Text(
                text = "忘记密码了？",
                color = InstagramBlue,
                fontSize = 14.sp,
                fontWeight = FontWeight.Bold,
                modifier = Modifier.clickable { /* 忘记密码处理 */ }
            )
        }
        // 登录按钮
        LoginButton1(
            inputUserName = inputUserName,
            inputPassword = inputPassword,
            onLoginSuccess = { user ->
//                userInfo = user
//                loginManager.saveUserInfo(user)
            },
            onLoginFailure = { showSnackbar = true },
        )
        // 分割线
        Divider(modifier = Modifier.fillMaxWidth().padding(vertical = 20.dp))
        // 注册文本按钮
        Row(
            modifier = Modifier
                .fillMaxWidth()
                .padding(vertical = 10.dp),
            horizontalArrangement = Arrangement.Center
        ) {
            Text(
                text = "还没有帐户？",
                color = InstagramDarkGray,
                fontSize = 14.sp,
                fontWeight = FontWeight.Bold
            )
            Spacer(modifier = Modifier.size(8.dp))
            Text(
                text = "注册",
                color = InstagramBlue,
                fontSize = 14.sp,
                fontWeight = FontWeight.Bold,
                modifier = Modifier.clickable { /* 注册处理 */ }
            )
        }
    }

    // 显示 Snackbar
    SnackbarHost(hostState = snackbarHostState)

    // 触发 Snackbar 显示
    LaunchedEffect(showSnackbar) {
        if (showSnackbar) {
            snackbarHostState.showSnackbar("帐户或密码错误，请重试")
            showSnackbar = false
        }
    }
}



@Composable
fun LoginButton1(
    inputUserName: String,
    inputPassword: String,
    onLoginSuccess: (UserInfo) -> Unit,
    onLoginFailure: () -> Unit,
) {
    Button(
        onClick = {
            // 模拟数据库
            val databaseUserData = listOf(UserInfo())
            val user = databaseUserData.find { it.userName == inputUserName && it.password == inputPassword }
            if (user != null) {
                // 登录成功
                Log.d("Login", "登录成功")
                onLoginSuccess(user)
                //loggedAccountList.add(user)
            } else {
                // 登录失败，显示错误信息
                Log.e("Login", "登录失败")
                onLoginFailure()
            }
        },
        colors = ButtonDefaults.buttonColors(
            containerColor = InstagramBlue,
            contentColor = Color.White
        ),
        modifier = Modifier
            .height(60.dp)
            .padding(horizontal = 8.dp)
            .fillMaxWidth(),
        shape = RoundedCornerShape(5.dp),
        elevation = ButtonDefaults.buttonElevation(defaultElevation = 0.dp),
        contentPadding = PaddingValues(horizontal = 8.dp, vertical = 0.dp)
    ) {
        Text(
            text = "登录",
            fontSize = 16.sp,
            color = Color.White,
            fontWeight = FontWeight.Bold
        )
    }
}

@Composable
fun LoginInput1(
    inputUserName: String,
    onUserNameChange: (String) -> Unit,
    inputPassword: String,
    onPasswordChange: (String) -> Unit
) {
    var isPasswordVisible by remember { mutableStateOf(false) }

    Column(
        modifier = Modifier.padding(start = 8.dp, end = 8.dp)
    ) {
        // 账号
        TextField(
            value = inputUserName,
            onValueChange = onUserNameChange,
            placeholder = {
                Text(
                    text = "手机号、账号或邮箱",
                    color = InstagramDarkGray,
                    fontSize = 14.sp
                )
            },
            modifier = Modifier
                .fillMaxWidth()
                .padding(vertical = 8.dp)
                .heightIn(min = 48.dp, max = 56.dp),
            colors = TextFieldDefaults.colors(
                focusedIndicatorColor = LightGrayBackground,
                unfocusedIndicatorColor = LightGrayBackground,
                disabledIndicatorColor = LightGrayBackground,
                unfocusedContainerColor = LightGrayBackground,
            ),
            shape = RoundedCornerShape(5.dp),
        )
        Spacer(modifier = Modifier.size(10.dp))
        // 密码
        TextField(
            value = inputPassword,
            onValueChange = onPasswordChange,
            placeholder = {
                Text(
                    text = "密码",
                    color = InstagramDarkGray,
                    fontSize = 14.sp
                )
            },
            modifier = Modifier
                .fillMaxWidth()
                .padding(vertical = 8.dp)
                .heightIn(min = 48.dp, max = 56.dp),
            colors = TextFieldDefaults.colors(
                focusedIndicatorColor = LightGrayBackground,
                unfocusedIndicatorColor = LightGrayBackground,
                disabledIndicatorColor = LightGrayBackground,
                unfocusedContainerColor = LightGrayBackground,
            ),
            shape = RoundedCornerShape(5.dp),
            visualTransformation = if (isPasswordVisible) VisualTransformation.None else PasswordVisualTransformation(),
            trailingIcon = {
                val image = if (isPasswordVisible) R.drawable.eye else R.drawable.eye_hide
                IconButton(onClick = { isPasswordVisible = !isPasswordVisible }) {
                    Icon(
                        painter = painterResource(id = image),
                        contentDescription = if (isPasswordVisible) "Hide Password" else "Show Password",
                        tint = Color.Unspecified,
                        modifier = Modifier.size(25.dp)
                    )
                }
            }
        )
    }
}

@Composable
fun LOGO1() {
    Image(
        painter = painterResource(id = R.drawable.ins),
        contentDescription = "logo",
        modifier = Modifier
            .size(200.dp)
            .padding(top = 50.dp)
    )
}


@Composable
fun SwitchAccountLoginPage() {
    //登录过的账号
    val loggedAccountList = remember { mutableListOf<UserInfo>() }
    Login1(loggedAccountList)
}
@Preview
@Composable
fun SwitchAccountLoginPagePreview() {
    SwitchAccountLoginPage()
}

